﻿using MahApps.Metro.Controls;
using MaterialDesignThemes.Wpf;
using System;
using System.Windows;
using System.Windows.Controls;
using Restaurant.Client.Framework;
namespace Restaurant.Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MetroWindow
    {


        public MainWindow()
        { 
            InitializeComponent();
            //tab_Main.ClosingItemCallback = new Dragablz.ItemActionCallback((x) =>
            //{

            //});
            //DataContext = new MainWindowView();
            //var op = new OperationResult();
            //op.Message = "";

        }

        private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {


        }


        /// <summary>
        /// 打开页面
        /// </summary>
        /// <param name="content"></param>
        /// <param name="tabname"></param>
        private void AddNewItem<T>(string tabname) where T : new ()
        {
            //如果以打开退出
            if (CheckOpened(tabname))
                return; 

            AddTab(new T () as Page, tabname);
        }

        private void AddNewItem(Type t, string tabname)
        {
            //如果以打开退出
            if (CheckOpened(tabname))
                return;

            Page p = Activator.CreateInstance(t) as Page;
            AddTab(p, tabname);
        }

        private bool CheckOpened(string tabname)
        {
            foreach (TabItem item in tab_Main.Items)
            {
                if (item.Header.ToString() == tabname)
                {
                    tab_Main.SelectedItem = item;
                    return true;
                }
            }
            return false;

        }

        private void AddTab(Page page, string tabname)
        {
            //添加标签
            TabItem tnew = new TabItem();

            tnew.IsTabStop = false;

            var f = new Frame();
            f.Content = page;

            tnew.Content = f;
            tnew.Header = tabname;

            tab_Main.Items.Add(tnew); 
            tab_Main.SelectedIndex = tab_Main.Items.Count -1;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            var b = (Button)sender;
            var t = b.Tag as Type; 
          
            AddNewItem (t, ((Button)sender).Content.ToString ());
        }

       
    }
}
